<?
require('../bootstrap.php');
require('../includes/site_config.php');
auth_redirect();

$pages = $db->get_result("SELECT `id`, `title`, `name`, `viewed` FROM $pages_table ORDER BY `index` ASC");

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>KomitCMS | Sider</title>
		<meta http-equiv="content-type" content="text/html;charset=utf-8" />
		<meta http-equiv="Content-Style-Type" content="text/css" />
		<link rel="stylesheet" type="text/css" href="../stylesheet/style.css" />
		<script type="text/javascript" src="../javascript/prototype.js"></script>
		<script src="../javascript/scriptaculous.js?load=effects" type="text/javascript"></script>
		<script type="text/javascript">
			
			window.onload = function() {
				loadAndRenderTable();
				if ($('notice')) {
					setTimeout("Effect.Fade('notice')", 2000);
				}
			}
		
			function tooglePageView(page_id) {
				new Ajax.Request('ajax.php', {
					method: 'post',
					parameters: {
						'method': 'tooglePageView',
						'page_id': page_id
					},
					onSuccess: function(transport) {
						if (transport.responseText.match(/FAILURE/)) {
							alert('Sidens status kunne ikke ændres!');
						} else if (transport.responseText.match(/SUCCESS/)) {
							//alert('Sidens status ændret!');
						}
					}
				});
			}
			function movePage(direction, id) {
				setLoading(true);
				new Ajax.Request('ajax.php', {
					method: 'post',
					parameters: {
						'method': 'movePage',
						'page_id': id,
						'direction': direction
					},
					onSuccess: function(transport) {
						// alert(transport.responseText);
						if (!transport.responseText.match(/SUCCESS/)) {
							alert("Kunne ikke rykke siden.");
						} else {
							loadAndRenderTable({
								highligth_row: id
							});
							setLoading(false);
						}
					}
				});
			}
			function setLoading(loading) {
				if (loading) {
					$('pages_table').hide();
					$('ajax_working').show();
				} else if (!loading) {
					$('ajax_working').hide();
					$('pages_table').show();
				}
			}
			function loadAndRenderTable(options) {
				new Ajax.Request('ajax.php', {
					method: 'post',
					parameters: {
						'method': 'retrievePages'
					},
					onSuccess: function(transport) {
						//alert(transport.responseText);
						var data = eval(transport.responseText);
						var tb = $('pagesTableBody');
						tb.update('');
						if (data != null && data.length > 0) {
							for (var i=0; i<data.length; i++) {
								var p = data[i];
								var tr = new Element('tr');
								tr.id  = 'row_' + p.id;
								if (i%2 != 0) {
									tr.addClassName("odd");
								}
								// Title column
								var td_title = new Element('td');
								td_title.insert(p.title);
								// Up-down buttons column
								var td_updown = new Element('td');
								td_updown.setAttribute('style', 'text-align: center');
								td_updown.insert(upButton(p.id));
								td_updown.insert(downButton(p.id));
								// Viewed checkbox column
								var td_viewed = new Element('td');
								td_viewed.setAttribute('style', 'text-align: center');
								td_viewed.insert(viewedCheckbox(p.id, p.viewed));
								// Edit button column
								var td_edit = new Element('td');
								td_edit.insert(editButton(p.id));
								// Delete button column 
								var td_delete = new Element('td');
								td_delete.insert(deleteButton(p.id));
								tr.appendChild(td_title);
								tr.appendChild(td_updown);
								tr.appendChild(td_viewed);
								tr.appendChild(td_edit);
								tr.appendChild(td_delete);
								tb.appendChild(tr);
							}
							if (options.highligth_row) {
								new Effect.Highlight('row_' + options.highligth_row);
							}
						}
					}
				});
			}
			
			function upButton(id) {
				var btn = '<button style="background:none;border:none;padding:3px;" title="Ryk siden op" onclick="movePage(\'UP\', '+ id + ')"><img src="../images/arrow_up.gif" /></button>';
				return btn;
			}
			function downButton(id) {
				var btn = '<button style="background:none;border:none;padding:3px;" title="Ryk siden op" onclick="movePage(\'DOWN\', '+ id + ')"><img src="../images/arrow_down.gif" /></button>';
				return btn;
			}
			function viewedCheckbox(id, isChecked) {
				var checked = (isChecked == 1) ? 'checked="checked"' : '';
				return '<input type="checkbox" '+ checked +' onchange="tooglePageView('+ id +')" /></td>';
			}
			function editButton(id) {
				return '<input type="button" class="AUI_TableButton" value="Ret" onclick="document.location.href=\'edit_page.php?id='+ id +'\'" />';
			}
			function deleteButton(id) {
				return '<form method="post" action="delete_page.php" onsubmit="return confirm(\'Er du sikker på du vil slette denne side?\')"><input type="hidden" name="id" value="'+ id +'" /><input type="submit" class="AUI_TableButton" value="Slet" onclick="" /></form>';
			}
			
		</script>
	</head>
	<body>
		<div class="AUI_Header">
			<h1>Admin</h1>
			<? include 'menu.php' ?>
		</div>
		<div class="AUI_Content">
			<h2>Sider</h2>
			
			<? if ($session->is_set('page_saved')) { 
				$session->del('page_saved');
				?>
				<div id="notice" class="UI_Notice_OK">
					<p>Siden er gemt</p>
				</div>
			<? } ?>
			
			<div id="ajax_working" style="display:none;">
				<img src="../images/ajax-loader.gif" />
			</div>
			
			<div class="UI_Table" id="pages_table">
				<table cellspacing="0" cellpadding="0" style="">
					<thead>
						<tr>
							<th>Titel</th>
							<th width="50px"></th>
							<th width="30px">Vis</th>
							<th width="50px"></th>
							<th width="50px" class="last"></th>
						</tr>
					</thead>
					<tbody id="pagesTableBody">
					</tbody>
				</table>
			</div>
		</div>
	</body>
</html>